<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
        :modal-class="my-modal"
        dialog-class="modal-dialog"
        @hidden 关闭窗体触发事件
    -->
    <b-modal ref="my-modal" :title="title" scrollable content-class="fullWinBody" @hidden="onHidden" size="fullWin" centered
        hide-footer no-close-on-backdrop>
        <div class="d-block text-center">
            <div class="card card-custom gutter-b example example-compact" id="tableInstanceBody">                
                <div class="card-body">
                    <!--查询条件-->
                    <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                        <div class="m-portlet__body">
                            <div class="form-group m-form__group row">
                                <label class="col-form-label">运行状态:</label>
                                <div class="col-md-2">
                                    
                                    <el-select v-model="searchForm.instanceType" placeholder="请输入">
                                        <el-option
                                            v-for="item in [{value:'',label:'已完成'},{value:1,label:'运行中'}]"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                        </el-option>
                                    </el-select>
                                </div>
                                <b-button :pressed="false" variant="btn btn-primary font-weight-bold mr-2"
                                    @click="search()"><span><i class="fa fa-search"></i><span>查询</span></span></b-button>
                                <b-button :pressed="false" variant="btn btn-light font-weight-bold mr-2"
                                    @click="resetAll()">
                                    <span><span>清空条件</span></span></b-button>
                            </div>
                        </div>
                        <div class="m-portlet__foot m-portlet__no-border m-portlet__foot--fit">
                            <div class="m-form__actions m-form__actions--solid">
                                <div class="row">
                                    <div class="col m--align-left">
                                        
                                    </div>
                                    <div class="col m--align-right">

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!--分页组件-->
                <el-table style="width: 100%" stripe border @selection-change="handleSelectionChange" highlight-current-row
                    :data="dataList">
                    <!-- 复选框 -->
                    <el-table-column type="selection" width="40"></el-table-column>
                    <el-table-column label="序号" min-width="50">
                        <template slot-scope="scope">
                            {{ scope.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column align="left" prop="processDefinitionName" show-overflow-tooltip label="流程名称"></el-table-column>
                    <el-table-column align="left" prop="businessKey" show-overflow-tooltip label="业务编号"></el-table-column>
                    <el-table-column align="left" prop="processInstanceId" show-overflow-tooltip label="流程实例">
                        <template slot-scope="scope">
                            <b-badge class="mr-1" variant="success">{{scope.row.processInstanceId}}</b-badge>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="startTime" show-overflow-tooltip  :formatter="formatterCreateTime" label="开始时间" min-width="150"></el-table-column>
                    <el-table-column align="center" prop="endTime" show-overflow-tooltip :formatter="formatterEndTime" label="结束时间" min-width="180"></el-table-column>
                    <el-table-column align="center" prop="durationInMillis" :formatter="formateDhms" label="耗时" min-width="150"></el-table-column>
                    <el-table-column align="center" prop="suspended" label="状态" min-width="100">
                        <template slot-scope="scope">
                            <div v-if="scope.row.suspended == true" class="symbol symbol-30 symbol-circle symbol-light-danger">
                                <span class="symbol-label font-size-h8">挂起</span>
                            </div>
                            <div v-else class="symbol symbol-30 symbol-circle symbol-light-primary">
                                <span class="symbol-label font-size-h8">激活</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column align="left" prop="tenantId" show-overflow-tooltip label="租户"></el-table-column>
                    <el-table-column align="center" label="操作" fixed="left" min-width="180">
                        <template slot-scope="scope">
                            <!--scope.row当前行的对象-->
                            <a href="javascript:;" @click="initLcAcitiviWin(scope.row)"
                                class="btn btn-sm btn-clean btn-icon" title="节点历史记录">
                                <span class="svg-icon svg-icon-primary svg-icon-2x">
                                    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
                                        width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                                        <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                                            <rect x="0" y="0" width="24" height="24" />
                                            <path
                                                d="M19.2078777,9.84836149 C20.3303823,11.0178941 21,12 21,12 C21,12 16.9090909,18 12,18 C11.6893441,18 11.3879033,17.9864845 11.0955026,17.9607365 L19.2078777,9.84836149 Z"
                                                fill="#000000" fill-rule="nonzero" />
                                            <path
                                                d="M14.5051465,6.49485351 L12,9 C10.3431458,9 9,10.3431458 9,12 L5.52661464,15.4733854 C3.75006453,13.8334911 3,
                                              12 3,12 C3,12 5.45454545,6 12,6 C12.8665422,6 13.7075911,6.18695134 14.5051465,6.49485351 Z"
                                                fill="#000000" fill-rule="nonzero" />
                                            <rect fill="#000000" opacity="0.3"
                                                transform="translate(12.524621, 12.424621) rotate(-45.000000) translate(-12.524621, -12.424621) "
                                                x="3.02462111" y="11.4246212" width="19" height="2" />
                                        </g>
                                    </svg>
                                </span>
                            </a>
                            <a href="javascript:;" @click="initLcApprovalWin(scope.row)"
                                class="btn btn-sm btn-clean btn-icon" title="审批记录">
                                <span class="svg-icon svg-icon-primary svg-icon-2x">
                                    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                                        <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                                            <rect x="0" y="0" width="24" height="24"/>
                                            <path d="M8.37867966,15.1213203 C9.35499039,16.0976311 9.35499039,17.6805435 8.37867966,18.6568542 L6.25735931,
                                            20.7781746 C5.28104858,21.7544853 3.69813614,21.7544853 2.72182541,
                                            20.7781746 C1.74551468,19.8018639 1.74551468,18.2189514 2.72182541,17.2426407 L4.84314575,15.1213203 C5.81945648,
                                            14.1450096 7.40236893,14.1450096 8.37867966,15.1213203 Z M3.81784105,19.7528699 C4.30599642,20.2410253 5.09745264,
                                            20.2410253 5.58560801,19.7528699 C6.07376337,19.2647145 6.07376337,18.4732583 5.58560801,17.9851029 C5.09745264,
                                            17.4969476 4.30599642,17.4969476 3.81784105,17.9851029 C3.32968569,18.4732583 3.32968569,19.2647145 3.81784105,19.7528699 Z" fill="#000000" opacity="0.3"/>
                                            <path d="M14.3890873,1.33273811 L22.1672619,9.1109127 C22.9483105,9.89196129 22.9483105,11.1582912 22.1672619,
                                            11.9393398 L12.9748737,21.131728 C12.1938252,21.9127766 10.9274952,21.9127766 10.1464466,21.131728 L2.36827202,
                                            13.3535534 C1.58722343,12.5725048 1.58722343,11.3061748 2.36827202,10.5251263 L11.5606602,1.33273811 C12.3417088,0.551689527 13.6080387,0.551689527 14.3890873,1.33273811 Z" fill="#000000"/>
                                        </g>
                                    </svg>
                                </span>
                            </a>
                            <a href="javascript:;" @click="showLcProcessInstance(scope.row)"
                                class="btn btn-sm btn-clean btn-icon" title="监控">
                                <span class="svg-icon svg-icon-primary svg-icon-2x">
                                    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                                        <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                                            <rect x="0" y="0" width="24" height="24"/>
                                            <circle fill="#000000" opacity="0.3" cx="20.5" cy="12.5" r="1.5"/>
                                            <rect fill="#000000" opacity="0.3" transform="translate(12.000000, 6.500000) rotate(-15.000000) translate(-12.000000, -6.500000) " x="3" y="3" width="18" height="7" rx="1"/>
                                            <path d="M22,9.33681558 C21.5453723,9.12084552 21.0367986,9 20.5,9 C18.5670034,9 17,10.5670034 17,12.5 C17,
                                            14.4329966 18.5670034,16 20.5,16 C21.0367986,16 21.5453723,
                                            15.8791545 22,15.6631844 L22,18 C22,19.1045695 21.1045695,20 20,20 L4,20 C2.8954305,20 2,19.1045695 2,18 L2,
                                            6 C2,4.8954305 2.8954305,4 4,4 L20,4 C21.1045695,4 22,4.8954305 22,6 L22,9.33681558 Z" fill="#000000"/>
                                        </g>
                                    </svg>
                                </span>
                            </a>
                            <a href="javascript:;" @click="showLcProcessInstanceBase64(scope.row)"
                                class="btn btn-sm btn-clean btn-icon" title="流程图" v-if="scope.row.end == false">
                                <span class="svg-icon svg-icon-primary svg-icon-2x">
                                    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                                        <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                                            <polygon points="0 0 24 0 24 24 0 24"/>
                                            <path d="M6,5 L18,5 C19.6568542,5 21,6.34314575 21,8 L21,17 C21,
                                            18.6568542 19.6568542,20 18,20 L6,20 C4.34314575,20 3,18.6568542 3,17 L3,8 C3,
                                            6.34314575 4.34314575,5 6,5 Z M5,17 L14,17 L9.5,11 L5,17 Z M16,14 C17.6568542,
                                            14 19,12.6568542 19,11 C19,9.34314575 17.6568542,8 16,8 C14.3431458,8 13,
                                            9.34314575 13,11 C13,12.6568542 14.3431458,14 16,14 Z" fill="#000000"/>
                                        </g>
                                    </svg>
                                </span>
                            </a>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="block">
                    <el-pagination hide-on-single-page @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="pageNo" :page-sizes="[5, 10, 30, 50]" :page-size="pageSize"
                        layout="->,total, sizes, prev, pager, next, jumper" :total="totalCount">
                    </el-pagination>
                </div>
            </div>
        </div>
        <LcInstancActivitiWin ref="LcInstancActivitiWinRef"></LcInstancActivitiWin>
        <LcApprovalWin ref="lcApprovalRef"></LcApprovalWin>
        <LcTaskHisWin ref="lcTaskHisWinRef"></LcTaskHisWin>
        <LcInstanceImageWin ref="lcInstanceImageWinRef"></LcInstanceImageWin>
        <template slot="modal-footer">
            <!--自定义按钮-->
        </template>
    </b-modal>
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js";

import LcInstancActivitiWin from "@/view/workflow/lc-components/lc-instance-activiti-win.vue";
import LcApprovalWin from "@/view/workflow/lc-components/lc-approval-win.vue";
import LcTaskHisWin from "@/view/workflow/lc-components/lc-image-win.vue";
import LcInstanceImageWin from "@/view/workflow/lc-components/lc-instance-image-win.vue";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating,dateformat } from "@/core/util/jehcUtil.js";
export default {
    data() {
        return {
            currentRow:null,
            title: "运行实例【",
            lcProcessId: "",
            searchForm:{instanceType:1,deployment_id:"",},
            dataList: [],
            sels: [], //当前选框选中的值
            pageNo: 1,      // 默认当前是第一页
            pageSize: 5,    // 当前每页的数据是5条
            totalCount: 0   // 总数默认为0
        }
    },
    components: {
        LcInstancActivitiWin,
        LcApprovalWin,
        LcTaskHisWin,
        LcInstanceImageWin,
    },
    watch: {
    },
    mounted() {

    },
    methods: {
        initList() {
            showWating({ renderBody: "tableInstanceBody" });
            var params = {};
            this.searchForm.deploymentId = this.currentRow.deploymentId;
            params.usePageNo = true;//采用第几页进行分页（兼容）
            params.pageSize = this.pageSize;//页面显示记录条数，在页面显示每页显示多少项的时候
            params.pageNo = this.pageNo;//开始的记录序号   
            params.searchJson = JSON.stringify(this.searchForm);//为form元素     
            apiUtil.queryPage(process.env.VUE_APP_WORKFLOW_API + "/lcInstance/list", params).then(({ data }) => {
                this.dataList = data.data;//给结果集赋值
                this.totalCount = data.total;// 获取当前数据的总数    
            });
        },
        handleSelectionChange(sels) {//获取选中的值
            this.sels = sels;
            console.log("选中的值", sels.map((item) => item.id));
        },
        showLcProcessInstanceBase64(row) {//流程活动图
            this.$refs.lcInstanceImageWinRef.showModal(row);
        },
        showModal(row) {
            this.$refs['my-modal'].show();
            this.lcProcessId = "";
            this.title = "运行实例【" + row.deploymentName + "】";
            this.currentRow = row;
            this.resetAll() ;
            this.initList();
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        onHidden() {//关闭窗体触发事件
            this.$emit("change", null);
        },
        handleSizeChange(val) { // 修改每页所存数据量的值所触发的函数
            this.pageSize = val;  // 修改页的大小
            this.initList();       // 按新的pageNo和pageSize进行查询
        },
        handleCurrentChange(val) { // 修改当前页所触发的函数
            this.pageNo = val;       // 更新当前的页
            this.initList();          // 按新的pageNo和pageSize进行查询
        },
        search() {
            this.pageNo = 1;       // 更新当前的页
            this.initList();          // 按新的pageNo和pageSize进行查询
        },
        resetAll() {
            Object.assign(this.$data.searchForm, this.$options.data().searchForm);
            this.search();
        },
        showLcProcessInstance(row) {//流程监控
            row.processTitle = row.processDefinitionName;
            this.$refs.lcTaskHisWinRef.showModal(row);
        },
        formateDhms(row){
            let timestamp = row.durationInMillis;
            if (timestamp) {
                timestamp = timestamp/1000;
                var result = '';
                if (timestamp >= 86400) {
                    var days = Math.floor(timestamp / 86400);
                    timestamp = timestamp % 86400;
                    result = days  + ' 天';
                    if (timestamp > 0) {
                        result += ' ';
                    }
                }
                if (timestamp >= 3600) {
                    var hours = Math.floor(timestamp / 3600);
                    timestamp = timestamp % 3600;
                    result += hours + ' 小时';
                    if (timestamp > 0) {
                        result += ' ';
                    }
                }
                if (timestamp >= 60) {
                    var minutes = Math.floor(timestamp / 60);
                    timestamp = timestamp % 60;
                    result += minutes + ' 分钟';
                    if (timestamp > 0) {
                        result += ' ';
                    }
                }
                result += parseInt(timestamp) + ' 秒';
                return result;
            }
            return "";
        },
        formatterCreateTime(row, column){
            return dateformat(row.startTime);
        },
        formatterEndTime(row, column){
            return dateformat(row.endTime);
        },
        initLcAcitiviWin(row){//历史            
            this.$refs.LcInstancActivitiWinRef.showModal(row);
        },
        initLcApprovalWin(row) {//审批记录
            this.$refs.lcApprovalRef.showModal(row.processInstanceId);
        }, 
    }
}
</script>